﻿
using Microsoft.AspNetCore.Mvc.ModelBinding;
using MS.Core.BaseProvider;
using MS.Core.Model;
using Newtonsoft.Json.Linq;
using NPOI.SS.Formula.Functions;
using ReportDesignerServer.DataModel;
using ReportDesignerServer.DataModels;
using ReportDesignerServer.Dto;
using ReportDesignerServer.IRepository;
using SqlSugar;

namespace ReportDesignerServer.Services
{
    /// <summary>
    /// 报表打印服务
    /// </summary>
    public class HiPrintService : ServiceBase<SysPrintData, IPrintDataRepository>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Repository"></param>
        public HiPrintService(IPrintDataRepository Repository)
        {
            this.Repository = Repository;
        }

        /// <summary>
        /// 获取所有打印模版
        /// </summary>
        /// <returns></returns>
        public List<SysHiprint> GetAllPrintList()
        {
            return this.Repository.DB.Queryable<SysHiprint>().ToList();
        }

        /// <summary>
        /// 获取所有打印模版的数据源配置
        /// </summary>
        /// <returns></returns>
        public List<SysPrintData> GetPrintDataListByName(string modelName)
        {
            Expressionable<SysPrintData> express = new Expressionable<SysPrintData>();
            express.And(c => c.printTempName == modelName);
            return this.Repository.GetListByWhere(express);
        }

        /// <summary>
        /// 通过名字获取打印记录
        /// </summary>
        /// <returns></returns>
        public SysHiprint GetSysHiprintByName(string tempName)
        {
            return this.Repository.DB.Queryable<SysHiprint>().Where(c => c.templateCode == tempName).First();
        }

        /// <summary>
        /// 保存打印数据
        /// </summary>
        /// <param name="sysHiprint"></param>
        /// <returns></returns>
        public bool SaveSysHiprintByName(SysHiprint sysHiprint)
        {
            long res;
            if (sysHiprint.id == 0)
            {
                res = Repository.DB.Insertable<SysHiprint>(sysHiprint).ExecuteReturnSnowflakeId();
            }
            else
                res = Repository.DB.Updateable<SysHiprint>(sysHiprint).ExecuteCommand();
            if (res > 0)
                return true;
            else return false;
        }


        /// <summary>
        /// 获取打印模版数据源实体
        /// </summary>
        /// <param name="modelName"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public JObject GetPrintData(string modelName, object[]? args = null)
        {
            Expressionable<SysPrintData> express = new Expressionable<SysPrintData>();
            express.And(c => c.printTempName == modelName);
            var dataSrcList = Repository.GetListByWhere(express);
            JObject jObject = new JObject();
            dataSrcList?.ForEach(a =>
            {
                var sql = args == null ? a.dataSql : string.Format(a.dataSql, args);
                var data = Repository.DB.Ado.SqlQuery<dynamic>(sql);
                if (a.single == 1)
                    jObject.Add(a.dataSourceKey, JToken.FromObject(data.First()));
                else
                    jObject.Add(a.dataSourceKey, JToken.FromObject(data));
                //if (a.DataSrcType == 2)
                //{
                //    var data = datasrcRepository.RowToCol(sql);
                //    jObject.Add(a.DataSourceEnName, JToken.FromObject(data));
                //}
                //else
                //{
                //    var data = datasrcRepository.Select(sql);
                //    jObject.Add(a.DataSourceEnName, JToken.FromObject(data));
                //}

            });
            return jObject;
        }



        /// <summary>
        /// 保存打印数据源
        /// </summary>
        /// <param name="sysHiprint"></param>
        /// <returns></returns>
        public bool SavePrintDataSource(SysPrintData sysHiprint)
        {
            long res;
            if (sysHiprint.id == 0)
            {
                res = Repository.ExecuteReturnSnowflakeId(sysHiprint);
            }
            else
                res = Repository.Update(sysHiprint);
            if (res > 0)
                return true;
            else return false;
        }
    }
}
